Using messages

The message system provides a way to notify about events and exchange information. At the core of the message system is KzuMessageDispatcher. The dispatcher acts as a mediator between message senders and handlers. It maintains the list of active message handlers and the message queue. You can access the message dispatcher from any object with kzuObjectNodeGetMessageDispatcher().

Messages and arguments

Every message is represented by a KzuMessage object created by kzuMessageDispatcherCreateMessage(). Messages type describes the nature of event or information (for example, Click and ItemSelected), and arguments to describe the data associated within the message (for example, TouchCoordinates and ItemNumber). The type of a message is described with KzuMessageType objects.

Message arguments are described and accessed like properties with KzuPropertyType objects. kzuMessageSet*Argument() functions set and kzuMessageGet*ArgumentDefault() functions retrieve arguments with the underlying storage type for arguments Float, Bool, Vector3, and so on. If the message does not have the argument the kzuMessageGet*ArgumentDefault() functions return the default value registered in the property type. Kanzi provides the built-in message types in kzu_general_messages.h.

Because the dispatcher manages the lifetime of the message object and associated arguments, do not keep the references to those objects.

Timer messages

The message system has inbuilt support for timer messages. kzuMessageDispatcherAddTimerHandler() subscribes and kzuMessageDispatcherRemoveTimerHandler() unsubscribes a timer message. During subscription set the interval for the timer and the timer behavior:

Dispatching messages

There are two ways you can dispatch a message to the handlers, each dispatch methods has its advantages and disadvantages:

Use kzuMessageDispatcherDispatchMessage() and change to kzuMessageDispatcherDispatchMessageSynchronous() only if it improve application responsiveness or simplify logic.

Receiving messages

All messages in Kanzi are routed messages. When a message is dispatched the system walks though the scene graph from the root node to the target of the message in a process called tunnelling and then walks back in a process called bubbling. At each scene graph node passed the system looks for handlers for the dispatched message. This means that handlers can be installed at any place in the graph effectively intercepting messages before they reach their destination or gathering messages from many sources.

Bubbling is the phase where most of the messages should be handled, whereas Tunnelling is usually used for intercepting or filtering messages.

Receivers are implemented as functions and passed as pointers to kzuMessageDispatcherAddHandler(), kzuMessageDispatcherAddTunnellingHandler(). To provide context to the receiver implementation, provide a pointer to the user data at registration time. Use kzuMessageDispatcherRemoveHandler() to remove handlers.

Creating custom messages in Kanzi Studio

To create a custom message in Kanzi Studio:

In the Library right-click Property Types and select Property Type.

  1. In the Library press Alt and right-click Property Types and select Property Type.

    The Property Type Editor opens.
  2. In the Property Type Editor set:
  3. Click Save.
    You can now use this custom message in your application.

See also

Using input manipulators

Triggers

Using triggers

Messages, triggers, and actions